﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApp
{
    public partial class _Default : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["userName"] == null)
            {
                Response.Write("<script>alert('当前还未登录，请登录！！！');location.assign('Login.aspx');</script>");
            }

            if (!IsPostBack)
            {
                DisPlay();
            }
        }

        public void DisPlay()
        {
            string sql = "select * from Task";
            GridView1.DataSource = DbHelper.GetData(sql);
            GridView1.DataBind();
        }
        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            var id = GridView1.DataKeys[e.RowIndex].Value.ToString();
            string sql = "delete Task where Id=" + id;

            var res = DbHelper.UpdateData(sql);
            if (res > 0)
            {
                Response.Write("<script>alert('删除成功')</script>");
            }
            else
            {
                Response.Write("<script>alert('删除失败')</script>");
            }

            DisPlay();
        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            DisPlay();

        }

        protected void addBtn_Click(object sender, EventArgs e)
        {
            Response.Redirect("TaskAdd.aspx");
        }

        protected void selBtn_Click(object sender, EventArgs e)
        {
            var str = selTxb.Text.Trim();
            string sql = "select * from Task where TaskName like '%" + str + "%' or TaskContent like '%" + str + "%'";

            GridView1.DataSource=DbHelper.GetData(sql);
            GridView1.DataBind();
        }

        protected void clearBtn_Click(object sender, EventArgs e)
        {
            selTxb.Text = "";
            DisPlay();
        }
    }
}